class Solution {
public:
    int mySqrt(int x) {
        int left = 0;
        int right = x;
        int mid = (left + right) >> 1;
        while (right >= left) {
            if (mid * mid <= x & (mid + 1) * (mid + 1)>x)
                return mid;
            else if (mid*mid<x)
            {
                left = mid + 1;
                mid = (left + right) >> 1;
            }
            else {
                right = mid - 1;
                mid = (left + right) >> 1;
            }
        }
        return 0;
    }
};